package com.aaa.sbm.service;

import com.aaa.sbm.constants.ExceptionConstant;
import com.aaa.sbm.constants.ReturnStatus;
import com.aaa.sbm.dao.EmpDao;
import com.aaa.sbm.entity.Emp;
import com.aaa.sbm.util.CustomException;
import com.aaa.sbm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao empDao;
    @Override
    public Result list() {
        return new Result<List<Emp>>(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(),empDao.list());
    }

    @Override
    public Result add(Emp emp) {
        int add = empDao.add(emp);
        if(add<=0){
            throw new CustomException(ExceptionConstant.UNKOWN_EXCEPTION.getErrorCode(),
                    ExceptionConstant.UNKOWN_EXCEPTION.getErrorMessage());
        }
        return new Result<Integer>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),add);
    }

    @Override
    public Result delete(int empNo) {
        int delete = empDao.delete(empNo);
        if(delete<=0){
            throw new CustomException(ExceptionConstant.UNKOWN_EXCEPTION.getErrorCode(),
                    ExceptionConstant.UNKOWN_EXCEPTION.getErrorMessage());
        }
        return new Result<Integer>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),delete);
    }

    @Override
    public Result update(Emp emp) {
        int update = empDao.update(emp);
        if(update<=0){
            throw new CustomException(ExceptionConstant.UNKOWN_EXCEPTION.getErrorCode(),
                    ExceptionConstant.UNKOWN_EXCEPTION.getErrorMessage());
        }
        return new Result<Integer>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),update);
    }

    @Override
    public Result page(Map map) {
        Object pageNo = map.get("pageNo");
        Object pageSize = map.get("pageSize");
        if(pageNo==null || pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        int start=(Integer.valueOf(pageNo+"")-1)*Integer.valueOf(pageSize+"");
        int end=Integer.valueOf(pageNo+"")*Integer.valueOf(pageSize+"")+1;
        map.put("start",start);
        map.put("end",end);
        List<Emp> page = empDao.page(map);
        int i = empDao.pageCount(map);
        Map map1=new HashMap();
        map1.put("pageEmp",page);
        map1.put("total",i);
        return new Result<Map>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),map1);
    }

    @Override
    public Result listCount() {
        int i = empDao.listCount();
        return new Result<Integer>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),i);
    }


}
